﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.order
{
    public partial class StoreOut : BaseAdmin
    {

        dalCompany theCompany = new dalCompany();
        public string carmsg = string.Empty;
        public string carmsgFormat = "<tr align='center'> "
           + " <td width='5%' height='28' align='middle'>{0}</td> "
            + " <td width='30%' height='28' align='middle'>{1}</td> "
              + " <td width='30%' height='28' align='middle'>{2}</td> "
           + " <td align='middle' width='17%'>{3}</td> "
           + "  <td width='5%' align='middle' width='12%'>{4}</td> "
           + " <td align='middle' width='17%'>{5}</td> "
           + " </tr> ";


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ViewContent();
            }
        }

        void ViewContent()
        {
            string sCompanyId = Request.QueryString["companyid"];
            string sCardCodeFrom = Request.QueryString["cardCodeFrom"];
              string sCardCodeTo = Request.QueryString["cardCodeTo"];
            if (!string.IsNullOrEmpty(sCompanyId))
            {
                tbCompany oCompany = theCompany.GetModel(int.Parse(sCompanyId));
                if (oCompany != null)
                {
                    this.lblUtil.Text = oCompany.Name;
                }
                string sSQL = "select min(CardCode) as minCardCode,max(CardCode) as maxCardCode,count(CardCode) as CardCount,CardName,CardPrice,CompanyName"
                               + " from "
                               + " ( "
                               + " select CardCode,CT.Name as CardName ,C.CardPrice,p.[Name] as CompanyName "
                                + " from DB_Card C join tbCardType CT on C.CardTypeId = CT.id "
                               + "  Join tbCompany P on C.CompanyId = P.Id "
                               + " where C.CompanyId = " + WebSite.re.replace(sCompanyId);

                if (!string.IsNullOrEmpty(sCardCodeFrom))
                {
                    sSQL = sSQL + " and convert(decimal,C.CardCode) >= " + WebSite.re.replace(sCardCodeFrom);
                }
                if (!string.IsNullOrEmpty(sCardCodeTo))
                {
                    sSQL = sSQL + " and convert(decimal,C.CardCode) <= " + WebSite.re.replace(sCardCodeTo);
                }

                sSQL = sSQL  + " ) a "
                               + "  group by CardName,CardPrice,CompanyName ";

                DataTable table = new DataTable();
                table = SQLAccess.GreatDs(sSQL).Tables[0];
               int i = 0;
                foreach(DataRow dr in table.Rows)
                {
                    i++;
                    carmsg = carmsg + string.Format(carmsgFormat, (i + 1).ToString(),
                                            dr["CardName"],
                                            dr["CardPrice"],
                                            dr["minCardCode"] + "-" + dr["maxCardCode"],
                                            dr["CardCount"],
                                            "福利点点通");
                }


            }

            carmsg = carmsg + string.Format(carmsgFormat, "<b>合计</b>",
                                           "",
                                           "",
                                           "",
                                           "",
                                           "");
        }
    }
}